<?php
namespace IMT\EventBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
use IMT\EventBundle\Entity\Event;
use IMT\EventBundle\Entity\Alert;



class EventAlertHandler
{
    protected $form;
    protected $request;
    protected $em;
    protected $idEvent;
    

    public function __construct(Form $form, Request $request, EntityManager $em,  $idEvent)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->idEvent = $idEvent;
        
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() )
            {
                $this->onSuccess($this->form->getData());

                return true;
            }
        }

        return false;
    }

    public function onSuccess(Alert $alert)
    {
        
        $event = new Event;
        $event = $this->em->getRepository('IMTEventBundle:Event')->find($this->idEvent);
        
        $alert->setEvent($event);
        $this->em->persist($event);
        $this->em->persist($alert);
        $this->em->flush();
       
    }
}